<template>
  <div>
    <el-table :data="userList" border style="width: 100%">
      <el-table-column prop="uid" label="用户编号" width="320"></el-table-column>
      <el-table-column prop="username" label="用户名称"></el-table-column>
      <el-table-column prop="rolename" label="所属角色"></el-table-column>
      <el-table-column label="状态" width="80">
        <template slot-scope="item">
          <div>
            <el-tag v-if="item.row.status == 1" type="success">启用</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="item">
          <div>
            <el-button
              @click="edit(item.row.uid)"
              type="primary"
              size="mini"
              icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button
              type="danger"
              @click="del(item.row.uid)"
              size="mini"
              icon="el-icon-delete"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加分页器 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="size"
      :total="count"
      @current-change="changePage"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { deleteUser } from "@/util/axios";
export default {
  data() {
    return {};
  },
  mounted() {
    //页面一加载就获取用户列表
    this.changePageAction(1);
    //调取列表总数
    this.getCount();
  },
  computed: {
    ...mapGetters({
      userList: "user/getUserList",
      count: "user/getCount",
      size: "user/getSize",
    }),
  },
  methods: {
    ...mapActions({
      getUserList: "user/getUserListAction",
      getCount: "user/getCountAction",
      changePageAction: "user/changePageAction",
    }),
    //封装一个删除事件
    del(uid) {
      this.$confirm("你确定要删除这条数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调取删除接口
          deleteUser({ uid }).then((res) => {
            if (res.data.code == 200) {
              //重新调取用户列表
              this.getUserList();
              this.getCount();
              this.$message.success(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //封装一个点击编辑删除按钮事件
    edit(uid) {
      this.$emit("edit", uid);
    },
    //封装一个切换页码的函数方法
    changePage(n) {
      this.changePageAction(n);
    },
  },
};
</script>

<style lang="stylus" scoped>
.el-pagination
  float: right;
</style>